// 定义跟用户相关的路由
const Router = require('koa-router');
const router = new Router();
const { userLogin, userRegister, updateAvatar, updateNickname } = require('../controllers/index.js'); // 引入 userLogin 
const { sign } = require('../utils/jwt.js');
var jwt = require('jsonwebtoken');

router.prefix('/user')


// 登录接口
router.post('/login', async (ctx, next) => {
  const { username, password } = ctx.request.body
  try {
    const res = await userLogin(username, password)
    if (res.length > 0) {

      const data = {
        username: res[0].username,
        nickname: res[0].nickname,
        id: res[0].userId,
        avatar: res[0].avatar,
      }

      const token = sign({
        id: res[0].userId,
        username: res[0].username
      })

      ctx.body = {
        code: 200,
        data: data,
        token: token,
        message: '登录成功'
      }
    } else {
      ctx.body = {
        code: 200,
        data: [],
        message: '用户名或密码错误'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      data: error,
      message: '服务器异常'
    }
  }
})

router.post('/userRegister', async (ctx, next) => {
  const { username, password } = ctx.request.body
  // console.log(username, password);
  // console.log(res);
  try {
    const res = await userRegister(username, password)
    if (res.note === 1) {
      ctx.body = {
        code: 200,
        note: res.note,
        message: '用户名已存在'
      }
    } else {
      ctx.body = {
        code: 200,
        note: 2,
        message: '注册成功'
      }
    }
  } catch {
    ctx.body = {
      code: 500,
      message: '服务器异常'
    }
  }

})

// router.post('/uploadAvatarImg', async (ctx, next) => {
//   const { id, image } = ctx.request.body
//   console.log('头像上传');
//   console.log(id, image);
//   // console.log(image);
// })

// 判断用户 token 与 id 是否一致
router.get('/checkUserPermission', async (ctx, next) => {
  let token = ctx.request.headers.authorization
  const userId = Number(ctx.query.userId)
  if (token) {
    // 校验
    const decoded = jwt.verify(token, 'abc')
    if (userId === decoded.id) {    // 合法的 token
      ctx.body = {
        code: 200,
        permission: true,
        message: "token生效",
      }
    } else {
      ctx.body = {
        code: 200,
        permission: false,
        message: "token过期，请重新登录",
      }
    }
  } else {
    ctx.body = {
      code: 200,
      permission: false,
      message: "您还未登录，请先登录",
    }
  }
})

router.post('/updateAvatar', async (ctx, next) => {
  const { userId, image } = ctx.request.body
  try {
    const res = await updateAvatar(userId, image);
    if (res && res.affectedRows > 0) {
      ctx.body = {
        code: 200,
        message: '修改成功'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器异常'
    }
  }
})

router.post('/updateNickname', async (ctx, next) => {
  const { userId, nickname } = ctx.request.body
  try {
    const res = await updateNickname(userId, nickname);
    if (res && res.affectedRows > 0) {
      ctx.body = {
        code: 200,
        message: '修改成功'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器异常'
    }
  }
})

module.exports = {
  userRouter: router
};